﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DXApplicationTangche.原型.FormCheckedListBox.Thing;

namespace DXApplicationTangche.原型
{
    public partial class FormCheckedListBox : DevExpress.XtraEditors.XtraForm
    {
        private List<Thing> things = new List<Thing>();
        public FormCheckedListBox()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.绑定数据源();
        }

        private void 绑定数据源()
        {
            things.Clear();
            switch (this.comboBox1.Text.Trim())
            {
                case "服装":
                    this.things.Add(new Thing("quanxuan", "全选0", 0));
                    this.things.Add(new Thing("chenshan", "衬衫1", 1));
                    this.things.Add(new Thing("maoyi", "毛衣2", 2));
                    this.things.Add(new Thing("kuzi", "裤子3", 3));
                    this.things.Add(new Thing("duanku", "短裤4", 4));
                    this.things.Add(new Thing("qunzi", "裙子5", 5));
                    break;
                case "数码":
                    this.things.Add(new Thing("quanxuan", "全选0", 0));
                    this.things.Add(new Thing("xiangji", "相机1", 1));
                    this.things.Add(new Thing("bijiben", "笔记本3", 3));
                    this.things.Add(new Thing("shouji", "手机5", 5));
                    this.things.Add(new Thing("pingban", "平板2", 2));
                    this.things.Add(new Thing("diannao", "电脑4", 4));
                    break;
                case "零食":
                    this.things.Add(new Thing("quanxuan", "全选0", 0));
                    this.things.Add(new Thing("xiaohuanxiong", "小浣熊4", 4));
                    this.things.Add(new Thing("weilong", "卫龙2", 2));
                    this.things.Add(new Thing("kangshifu", "康师傅5", 5));
                    this.things.Add(new Thing("jianguo", "坚果1", 1));
                    this.things.Add(new Thing("xiaolongxia", "小龙虾3", 3));
                    break;
            }
            this.things.Sort(new ThingIndexComparer());
            this.checkedListBoxControl1.DisplayMember = "name";
            this.checkedListBoxControl1.ValueMember = "id";
            this.checkedListBoxControl1.DataSource = this.things;
        }
        private void ShowItemValues(DevExpress.XtraEditors.CheckedListBoxControl checkedListBoxControl)
        {
            StringBuilder sb = new StringBuilder();
            foreach (object item in checkedListBoxControl.CheckedItems)
            {
                Thing rv = item as Thing; //数据源是DataTable,对应的Item是DataRowView类型。

                sb.AppendLine("{" + rv.id.ToString() + "," + rv.name.ToString() + "};");
            }

            //MessageBox.Show("您选择的项目是：\r\n" + sb.ToString());
            this.richTextBox1.Text = this.richTextBox1.Text + sb.ToString();
        }

        private void checkedListBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void checkedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            this.ShowItemValues(this.checkedListBoxControl1);
            //MessageBox.Show(this.things[e.Index].name.ToString());
            if (this.checkedListBoxControl1.GetItemChecked(e.Index))
            {
                Thing thing = this.checkedListBoxControl1.GetItem(e.Index) as Thing;
                MessageBox.Show(thing.name);
            }
            
        }
        public class Thing
        {
            public String id { get; set; }
            public String name { get; set; }
            public int index { get; set; }
            public Image pic { get; set; }
            public Thing(String id, String name, int index)
            {
                this.id = id;
                this.name = name;
                this.index = index;
                this.pic = DXApplicationTangche.Properties.Resources.SSHIRT;
            }
            public class ThingIndexComparer : IComparer<Thing>
            {
                public int Compare(Thing p1, Thing p2)
                {
                    return p1.index.CompareTo(p2.index);
                }
            }
        }
    }
}
